package ae.pcfc.cldos.online.web.controller.client.company.payment;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.delegate.FnpGSDelegate;
import ae.pcfc.cldos.online.common.exception.GSAppException;
import ae.pcfc.cldos.online.common.exception.NoSuchCompanyException;
import ae.pcfc.cldos.online.common.vo.ep.peyment.PaymentAdviceRequestVO;
import ae.pcfc.cldos.online.common.vo.ep.peyment.PaymentAdviceResponseVO;
import ae.pcfc.cldos.online.fnp.ConstantsFNP;
import ae.pcfc.cldos.online.fnp.exception.FNPException;
import ae.pcfc.cldos.online.fnp.service.client.vo.FNPParameterVO;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BaseController;
import ae.pcfc.cldos.online.web.model.PaymentInfoModel;
import ae.pcfc.cldos.online.web.services.fnp.FnpParametersCheckService;

/**
 * 
 * @author arosa
 *
 */
@Controller
@RequestMapping(value=ViewPath.__PAYMENT_CONTROLLER)
public class PreviewPaymentController extends BaseController {

	@Autowired
	@Qualifier("FnpGSDelegate")
	private FnpGSDelegate fnpGSDelegate;
	
	@Autowired
	private FnpParametersCheckService fnpParametersCheckService;
	
	
	private static final String NOT_FEELED_FIELDS = "notFiledFields";
	private static final String REQUIRED_FIELDS = "requiredFields";
	private static final String ALL_PARAMS = "allParams";
	private static final String FORM_ID = "formId";
	private static final String CHECK_PARAMS_VIEW = "admin.general.payment.fnpParams";
	private static final String VIEW_PAYMENT_INFO_VIEW = "admin.general.payment.preview";
	
	@RequestMapping(method = RequestMethod.POST, value = "/preview.htm")
	public ModelAndView checkParams(@RequestParam String fnpServiceCode, 
									@RequestParam String formId,
									@RequestParam(required=false, defaultValue="0.0") String bgRequired,
									@RequestParam(required=false, defaultValue="false") String overLoadService,
									@RequestParam(required=false, defaultValue="false") boolean isFnpPreviewSubmit,
									ModelMap model,	HttpServletRequest request) throws Throwable{
		ModelAndView mav = null;
			List<FNPParameterVO> allParams = fnpGSDelegate.retreiveFNPParameters(Arrays.asList(new String[]{fnpServiceCode}));
			//addTestFnp(allParams);
			if(allParams!=null && allParams.size()!=0){
				List<FNPParameterVO> notFeeled = fnpParametersCheckService.feelParamsAndGetNotFeeled(request, allParams);  
				if(notFeeled.size()>0){
					if(isFnpPreviewSubmit){
						model.put(NOT_FEELED_FIELDS, notFeeled);
					}
					List<FNPParameterVO> requiredFields = fnpParametersCheckService.getRequiredParams(request, allParams);
					model.put(REQUIRED_FIELDS, requiredFields);
					model.put(FORM_ID, formId);
					mav = new ModelAndView(CHECK_PARAMS_VIEW); //:FIXME;
				}else{
					mav = preview(fnpServiceCode, model, allParams, bgRequired,overLoadService);
				}
			}else{
				mav = preview(fnpServiceCode, model, allParams, bgRequired,overLoadService);
			}
		return mav;
	}
	
	public ModelAndView preview(String fnpServiceCode, ModelMap model, List<FNPParameterVO> fnpParams,
			String bgRequired,String overLoadService) 
			throws Throwable {
		PaymentAdviceRequestVO paymentAdviceRequestVO = new PaymentAdviceRequestVO();
		paymentAdviceRequestVO.setFnpServiceCode(fnpServiceCode);
		paymentAdviceRequestVO.setUserVO(getUserVO());
		paymentAdviceRequestVO.setFnpParams(fnpParams);
		if(overLoadService.equals("true")){
			paymentAdviceRequestVO.setOverLoadService(true);
		}
		
		try{
			PaymentAdviceResponseVO fnpPaymentAdviceRspnsVO = 
					fnpGSDelegate.previewPaymentInformation(paymentAdviceRequestVO);
			PaymentInfoModel paymentInfoModel = new PaymentInfoModel();
			mapper.map(fnpPaymentAdviceRspnsVO, paymentInfoModel);
			paymentInfoModel.setOverLoadCharges(fnpPaymentAdviceRspnsVO.getOverLoadCharges());
			Double dBgRequired = null;
			try{
				dBgRequired = Double.parseDouble(bgRequired);
			}catch (Exception e) {
				dBgRequired = 0.0;
			}
			if(dBgRequired>fnpPaymentAdviceRspnsVO.getBankGuarenteeBalance()){
				paymentInfoModel.setBankGuarenteePayment(dBgRequired);
			}
			model.put("paymentInfoModel", paymentInfoModel);
			model.put(ALL_PARAMS, fnpParams);
		}catch(FNPException e){
			e.printStackTrace();
			model.put("errorInPreviewPayment", "fnp.inactive.msg");
		}catch(GSAppException e){
			e.printStackTrace();
			model.put("errorInPreviewPayment", "gs.inactive.msg");
		}catch(NoSuchCompanyException e){
			e.printStackTrace();
			model.put("errorInPreviewPayment", "ep.payment.license.error");
		} catch(Exception e){
			e.printStackTrace();
			model.put("errorInPreviewPayment", "ep.payment.preview.error");
		}
		
		return new ModelAndView(VIEW_PAYMENT_INFO_VIEW);
	}
}
